<template>
    <div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>用户名称</td>
                    <td><input type="text" v-model="add.name"></td>
                </tr>
                <tr>
                    <td>昵称</td>
                    <td><input type="text" v-model="add.nname"></td>
                </tr>
                <tr>
                    <td>会员等级</td>
                    <td>
                        <select v-model="add.sname">
                            <option value="">==请选择==</option>
                            <option v-for="a in list" :value="a.sname">{{ a.sname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>图片</td>
                    <td>
                    <td>
                        <input type="file" @change="imga">
                        <img :src="add.img" width="60" height="60" v-if="add.img != ''">
                    </td>
                    </td>
                </tr>

                <tr>
                    <td>使用次数</td>
                    <td><input type="number" v-model="add.age"></td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td><input type="date" v-model="add.time"></td>
                </tr>
                <tr>
                    <td>地址</td>
                    <td><input type="text" v-model="add.address"></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="add.tyle" name="tyle" :value=true>启用
                        <input type="radio" v-model="add.tyle" name="tyle" :value=false>禁用
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加会员" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from "vue";
import moment from 'moment';
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

onMounted(() => {
    ft();
})
const ft = () => {
    axios({
        url: 'https://localhost:7104/api/Emp/fth',
        method: 'get',
        params: {
            id: route.query.id
        }
    })
        .then(res => {
            console.log(res);
            Object.assign(add, res.data)
            add.time=res.data.time.substring(0,10);
        })
        .catch(err => {
            console.log(err);
        })
}
let add: any = reactive(
    {
        id: route.query.id,
        "name": "",
        "nname": "",
        "sname": "",
        "img": "",
        "age": '',
        "time": "",
        "address": "",
        "tyle": true
    }
)
const imga = (e: any) => {
    console.log(e);
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj);
    axios({
        url: 'https://localhost:7104/api/Emp/ImgAdd',
        method: 'post',
        data: f
    })
        .then(res => {
            console.log(res);
            if (res.data == "格式不正确") {
                alert("格式不正确");
                return;
            }
            if (res.data == "大于2M") {
                alert("大于2M");
                return;
            }
            add.img = res.data;
        })
        .catch(err => {
            console.log(err);

        })
}
let list: any = ref([]);
onMounted(() => {
    bing();
})
const bing = () => {
    axios({
        url: 'https://localhost:7104/api/Emp/bingh',
        method: 'get',
        params: add
    })
        .then(res => {
            console.log(res);
            list.value = res.data;
        })
        .catch(err => {
            console.log(err);

        })
}
const ok = () => {
    axios(
        {
            url: 'https://localhost:7104/api/Emp/updateh',
            method: 'post',
            data: add
        }
    )
        .then(res => {
            console.log(res);
            if (res.data == -1) {
                alert("昵称已被使用");
                return;
            }
            if (res.data > 0) {
                alert("修改成功");
                router.push("/Hshow")
            }
        })
        .catch(err => {
            console.log(err);

        })
}
</script>

<style scoped></style>